# coding: utf-8

import Pyro4
Pyro4.config.HMAC_KEY = '123456' # set HMAC KEY for client.py

import getpass
from validator import Validator
import sys

import config

class Client():
    def __init__(self):
        self.user = Pyro4.Proxy("PYRONAME:server.user")
        self.meeting = Pyro4.Proxy("PYRONAME:server.meeting")
        self.validator = Validator()
        self.authenticatedUser = {}
        self.addMeetingInputChecked = \
        {
            "otherUsername": "",
            "start": "",
            "end": "",
            "title": ""
        }
        
        self.queryMeetingInputChecked = \
        {
            "start": "",
            "end": ""
        }
        
        self.deleteMeetingInputChecked = \
        {
            "meetingId": ""
        }
    
    def mainMenu(self):
        print "1. User register" + config.newLine + \
        "2. Add meeting" + config.newLine + \
        "3. Query meeting" + config.newLine + \
        "4. Delete meeting" + config.newLine + \
        "5. Clear meeting"
        
        choice = raw_input("Please select what you want to do:" + config.newLine + config.inputStart)
        
        if choice == "1":
            self.userRegistInput()
        elif choice == "2":
            self.userAuthenticationInput()
            #print "--------------------"
            self.addMeetingInput()
        elif choice == "3":
            self.userAuthenticationInput()
            self.queryMeetingInput()
        elif choice == "4":
            self.userAuthenticationInput()
            self.deleteMeetingInput()
        elif choice == "5":
            self.userAuthenticationInput()
            self.clearMeetingInput()
        else:
            print "Choice doesn't exist"
            self.mainMenu()
    
    def userRegistInput(self):
        selfFunc = "self.%s()" % (sys._getframe(0).f_code.co_name)
        
        username = raw_input("Username:" + config.newLine + config.inputStart)
        if self.validator.generalValidator({ "username": username }) == False:
            eval(selfFunc)
        
        password = getpass.getpass("Password" + config.passwordFieldTip + ":" + config.newLine + config.inputStart)
        confirmPassword = getpass.getpass("Confirm Password" + config.passwordFieldTip + ":" + config.newLine + config.inputStart)
        if self.validator.generalValidator({ "password": [ password, confirmPassword ] }) == False:
            eval(selfFunc)
            
        res = self.user.userRegist(username, password)
        if self.validator.speicalValidator(res) == False:
            eval(selfFunc)
        else:
            self.mainMenu()
            
    def userAuthenticationInput(self):
        selfFunc = "self.%s()" % (sys._getframe(0).f_code.co_name)
        
        username = raw_input("Username:" + config.newLine + config.inputStart)
        if self.validator.generalValidator({ "username": username }) == False:
            eval(selfFunc)
            return
        
        password = getpass.getpass("Password" + config.passwordFieldTip + ":" + config.newLine + config.inputStart)
        if self.validator.generalValidator({ "password": [ password ] }) == False:
            eval(selfFunc)
            return
        
        res = self.user.userAuthentication(username, password)
        if self.validator.speicalValidator(res) == False:
            eval(selfFunc)
            return
        else:
            userId = res["userAuthentication"]["returnVal"]["id"]
            self.authenticatedUser = { "id": userId, "username": username }
        
    def addMeetingInput(self):
        selfFunc = "self.%s()" % (sys._getframe(0).f_code.co_name)
        
        #print "checked-----------", self.addMeetingInputChecked
        
        if self.addMeetingInputChecked["otherUsername"] == "":
            otherUsername = raw_input("Other Username" + config.otherUsernameFieldTip + ":" + config.newLine + config.inputStart)
            if self.validator.generalValidator({ "otherUsername": [otherUsername, self.authenticatedUser] }) == False:
                eval(selfFunc)
                return
            
            res1 = self.user.otherUsernameIsExist(otherUsername)
            if self.validator.speicalValidator(res1) == False:
                eval(selfFunc)
                return
            else:
                self.addMeetingInputChecked["otherUsername"] = otherUsername
                
        if self.addMeetingInputChecked["start"] == "":
            start = raw_input("Start" + config.startEndFieldTip + ":" + config.newLine + config.inputStart)
            if self.validator.generalValidator({ "time": start }) == False:
                eval(selfFunc)
                return
            else:
                self.addMeetingInputChecked["start"] = start
            
        if self.addMeetingInputChecked["end"] == "":
            end = raw_input("End" + config.startEndFieldTip + ":" + config.newLine + config.inputStart)
            if self.validator.generalValidator({ "time": end }) == False:
                eval(selfFunc)
                return
            else:
                self.addMeetingInputChecked["end"] = end
        
        if self.addMeetingInputChecked["title"] == "":
            title = raw_input("Title:" + config.newLine + config.inputStart)
            if self.validator.generalValidator({ "title": title }) == False:
                eval(selfFunc)
                return
            else:
                self.addMeetingInputChecked["title"] = title
        
        res2 = self.meeting.addMeeting(self.authenticatedUser, self.addMeetingInputChecked)
        if self.validator.speicalValidator(res2) == False:
            for x in self.addMeetingInputChecked:
                self.addMeetingInputChecked[x] = ""
            eval(selfFunc)
            return
        else:
            self.authenticatedUser.clear()
            #print self.authenticatedUser
            self.mainMenu()
        
    def queryMeetingInput(self):
        selfFunc = "self.%s()" % (sys._getframe(0).f_code.co_name)
        
        if self.queryMeetingInputChecked["start"] == "":
            start = raw_input("Start" + config.startEndFieldTip + ":" + config.newLine + config.inputStart)
            if self.validator.generalValidator({ "time": start }) == False:
                eval(selfFunc)
                return
            else:
                self.queryMeetingInputChecked["start"] = start
            
        if self.queryMeetingInputChecked["end"] == "":
            end = raw_input("End" + config.startEndFieldTip + ":" + config.newLine + config.inputStart)
            if self.validator.generalValidator({ "time": end }) == False:
                eval(selfFunc)
                return
            else:
                self.queryMeetingInputChecked["end"] = end
        
        res = self.meeting.queryMeeting(self.authenticatedUser, self.queryMeetingInputChecked)
        if self.validator.speicalValidator(res) == False:
            for x in self.queryMeetingInputChecked:
                self.queryMeetingInputChecked[x] = ""
            eval(selfFunc)
            return
        else:
            self.authenticatedUser.clear()
            #print self.authenticatedUser
            self.mainMenu()
        
    def deleteMeetingInput(self):
        selfFunc = "self.%s()" % (sys._getframe(0).f_code.co_name)
        
        if self.deleteMeetingInputChecked["meetingId"] == "":
            meetingId = raw_input("Meeting id" + config.deleteFieldTip + ":" + config.newLine + config.inputStart)
            if self.validator.generalValidator({ "meetingId": meetingId }) == False:
                eval(selfFunc)
                return
            else:
                self.deleteMeetingInputChecked["meetingId"] = meetingId
        
        confirm = raw_input("Are you sure to delete that meeting" + config.confirmFieldTip + "?" + config.newLine + config.inputStart)
        if self.validator.generalValidator({ "confirm": confirm }) == False:
            eval(selfFunc)
            return
        
        if confirm == "Yes":
            res = self.meeting.deleteMeeting(self.authenticatedUser, self.deleteMeetingInputChecked)
            if self.validator.speicalValidator(res) == False:
                for x in self.deleteMeetingInputChecked:
                    self.deleteMeetingInputChecked[x] = ""
                eval(selfFunc)
                return
            else:
                self.authenticatedUser.clear()
                self.mainMenu()
        else:
            print "Meeting delete was cancelled"
            self.mainMenu()
        
    def clearMeetingInput(self):
        selfFunc = "self.%s()" % (sys._getframe(0).f_code.co_name)
        
        confirm = raw_input("Are you sure to clear all meeting organized by you" + config.confirmFieldTip + "?" + config.newLine + config.inputStart)
        if self.validator.generalValidator({ "confirm": confirm }) == False:
            eval(selfFunc)
            return
            
        if confirm == "Yes":
            res = self.meeting.clearMeeting(self.authenticatedUser)
            if self.validator.speicalValidator(res) == True:
                self.authenticatedUser.clear()
                self.mainMenu()
        else:
            print "Meeting clear was cancelled"
            self.mainMenu()
        
        
if __name__ == "__main__":
    client = Client()
    client.mainMenu()
